import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show SystemNavigator;
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/dao/login_dao.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/model/home/exam_key_model.dart';
import 'package:hktk_app/model/login/config_model.dart';
import 'package:hktk_app/model/my/my_choose_category_model.dart';
import 'package:hktk_app/navigotr/tab_navigator.dart';
import 'package:hktk_app/pages/login/automatic2_page.dart';
import 'package:hktk_app/util/device_util.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/login/ask_agreement_widget.dart';
import 'package:logging/logging.dart';

/// 获取设备信息请求页面
class DeviceAskPage extends StatefulWidget {
  const DeviceAskPage({super.key});

  @override
  State<DeviceAskPage> createState() => _DeviceAskPageState();
}

class _DeviceAskPageState extends State<DeviceAskPage> {
  late BuildContext _context;

  ConfigModel? _configModel;

  @override
  Widget build(BuildContext context) {
    _context = context;
    return Scaffold(
      backgroundColor: colorWhite1,
      body: FutureBuilder<Map<String, dynamic>>(
        future: _initData(),
        builder: (
          BuildContext context,
          AsyncSnapshot<Map<String, dynamic>> snapshot,
        ) {
          if (snapshot.connectionState == ConnectionState.done) {
            Map<String, dynamic> map = snapshot.data!;
            WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
              if(mounted){
                if (!map["success"]) {
                  ToastUtil.show(context: context, msg: map["msg"]);
                  SystemNavigator.pop();
                } else {
                  _askItem();
                }
              }
            });
            return Container();
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
  /// 初始化数据相关方法
  Future<Map<String, dynamic>> _initData() async {

    Map<String, dynamic> map = {};
    try {
      _configModel = await LoginDao.config();
      // _configModel =new ConfigModel();
      if (_configModel != null) {
        map["success"] = true;
      } else {
        map["success"] = false;
        map["msg"] = "获取协议数据失败";
      }
    } catch (e) {

      map = {"success": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        map["msg"] = e.msg;
      }
    }
    return map;
  }

  /// 请求方法
  void _askItem() {
    // Navigator.pop(result)的result怎么传给showDialog的呢
    ToastUtil.showDialogBox(
      context: _context,
      titleText: "感谢你使用我们的APP!",
      titleStyle: FontUtil.fontBlack1(
        20,
        fontWeight: FontWeight.w500,
      ),
      contentWidget: AskAgreementWidget(appConfig: _configModel!.appConfig!),
    ).then((bool? value) async {
      if (value == true) {
        DeviceUtil.deviceInit();
        if (getToken() == null) {
          Navigator.pushReplacementNamed(_context, "/login");
        } else {
          // 如果没有选择科目，则进入选择科目页面(防止某些操作出问题)
          // UserChooseCategoryVo userChooseCategoryVo =await MyDao.getCurrentChoose();
          // if(userChooseCategoryVo==null){
          // //   前往选择科目页面
          //   Navigator.pushReplacementNamed(_context, "/select-exam");
          // }else{
          //   // 设置本地
          //   if(IndustryDao.getSelectCategoryId()==null||IndustryDao.getSelectCategoryId()!=userChooseCategoryVo.categoryId){
          //     IndustryDao.saveSelectCategoryId(userChooseCategoryVo.categoryId);
          //     IndustryDao.saveExamKey(ExamKeyModel(industryId: userChooseCategoryVo.industryId,examId: userChooseCategoryVo.examId,examName: userChooseCategoryVo.examName,industryName: userChooseCategoryVo.industryName));
          //   }
          //   Navigator.pushReplacementNamed(_context, "/select-exam");
          //   // Navigator.pushReplacementNamed(_context, "/tab");
          // }

          Navigator.pushReplacementNamed(_context, "/select-exam");
        }
      } else {
        SystemNavigator.pop();
      }
    });
  }
}
